package com.test.schoolhelper.ui.activity;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.test.schoolhelper.R;
import com.test.schoolhelper.base.BaseActivity;
import com.test.schoolhelper.databinding.ActivityContainerBinding;
import com.test.schoolhelper.ui.fragment.NewsFragment;
import com.test.schoolhelper.ui.fragment.WeatherFragment;

/**
 * create by 20303 on 2024/6/9
 */
public class ContainerActivity extends BaseActivity<ActivityContainerBinding> {

    private LocalActivityManager localActivityManager;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        localActivityManager = new LocalActivityManager(this,true);
        localActivityManager.dispatchCreate(savedInstanceState);
    }


    @Override
    protected void onPause() {
        localActivityManager.dispatchPause(isFinishing());
        super.onPause();
    }
    @Override
    protected void onResume() {
        super.onResume();
        localActivityManager.dispatchResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        Intent intent = getIntent();
        if (intent != null){
            boolean isActivity = intent.getBooleanExtra("isActivity",true);
            String className = intent.getStringExtra("className");
            initContentType(isActivity,className);
        }
    }



    private void initContentType(boolean isActivity,String className){
        if (isActivity){
            Intent intent = new Intent(ContainerActivity.this,getActivityClass(className));
            View decorView = localActivityManager.startActivity("one", intent).getDecorView();
            getBinding().containerLayout.removeAllViews();
            getBinding().containerLayout.addView(decorView);
        }else {
            getSupportFragmentManager().beginTransaction().replace(R.id.container_layout,getFragmentByClassName(className)).commit();
        }
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_container;
    }

    @Override
    protected ActivityContainerBinding onCreateViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityContainerBinding.inflate(layoutInflater);
    }


    private Class<?> getActivityClass(String className){
        if (className.equals(MusicActivity.class.getName())){
            return MusicActivity.class;
        }else if (className.equals(AddScheduleActivity.class.getName())){
            return AddScheduleActivity.class;
        }else if (className.equals(AddAlarmActivity.class.getName())){
            return AddAlarmActivity.class;
        }else if (className.equals(UserActivity.class.getName())){
            return UserActivity.class;
        }else if (className.equals(CalculatorActivity.class.getName())){
            return CalculatorActivity.class;
        }else if (className.equals(MainActivity.class.getName())){
            return MainActivity.class;
        }else {
            throw new RuntimeException("找不到类名为：" + className + "的Activity！");
        }
    }
    
    private Fragment getFragmentByClassName(String className){
        if (className.equals(WeatherFragment.class.getName())){
            return new WeatherFragment();
        }else if (className.equals(NewsFragment.class.getName())){
            return new NewsFragment();
        }else {
            throw new RuntimeException("找不到类名为：" + className + "Fragment！");
        }
    }

    
}
